package com.estronger.shopmanage.adapter;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.estronger.shopmanage.bean.B9Share;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.home.WebActivity;
import com.soonfor.evaluate.base.EvalBaseAdapter;
import java.util.ArrayList;
import java.util.List;

public class MainListAdapter extends EvalBaseAdapter<MainListAdapter.MainListHoder, B9Share> {

	private List<B9Share> myList = new ArrayList<>();

	public MainListAdapter(Activity context) {
		super(context);
	}

	@NonNull
	@Override
	public MainListHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
		return new MainListHoder(mInflater.inflate(R.layout.main_item, parent, false));
	}

	@Override
	public void onBindViewHolder(@NonNull MainListHoder holder, int position) {
		final B9Share sData = myList.get(position);
		holder.setData(sData);
		holder.rlfArticleItem.setTag(position);
		holder.rlfArticleItem.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int pos = (int) view.getTag();
				try {
					if (myList.get(pos) != null) {
						Intent intent = new Intent(context, WebActivity.class);
						intent.putExtra("title", myList.get(pos).title);
						intent.putExtra("url", myList.get(pos).address);
						context.startActivity(intent);
					}
				} catch (Exception e) {}
			}
		});
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemCount() {
		return myList==null?0:myList.size();
	}

	@Override
	public void notifyDataSetChanged(List<B9Share> dataList) {
		myList = dataList;
		notifyDataSetChanged();
	}
	class MainListHoder extends RecyclerView.ViewHolder {
		RelativeLayout rlfArticleItem;
		TextView title, author, date, jinghua;
		ImageView imgfDeliver;

		public MainListHoder(View view) {
			super(view);
			this.rlfArticleItem = view.findViewById(R.id.rlfArticleItem);
			this.title = (TextView) view.findViewById(R.id.title);
			this.author = (TextView) view.findViewById(R.id.author);
			this.date = (TextView) view.findViewById(R.id.date);
			this.jinghua = (TextView) view.findViewById(R.id.jinghua);
			this.imgfDeliver = (ImageView) view.findViewById(R.id.imgfDeliver);
		}
		public void setData(B9Share sData){
			this.title.setText(sData.title);
			this.author.setText(sData.author);
			this.date.setText(sData.distributedate);
			if (sData.isessence) {
				this.jinghua.setVisibility(View.VISIBLE);
			}
		}
	}
}
